<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Category;
use Illuminate\Support\Str;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $categories = [
            [
                'name' => '技术分享',
                'slug' => 'tech',
                'description' => '分享最新的技术动态和开发经验',
                'parent_id' => null,
            ],
            [
                'name' => '生活随笔',
                'slug' => 'life',
                'description' => '记录生活中的点点滴滴',
                'parent_id' => null,
            ],
            [
                'name' => '学习笔记',
                'slug' => 'study',
                'description' => '学习过程中的心得体会和笔记整理',
                'parent_id' => null,
            ],
            [
                'name' => '项目经验',
                'slug' => 'project',
                'description' => '项目开发中的经验总结',
                'parent_id' => null,
            ],
        ];

        foreach ($categories as $categoryData) {
            Category::firstOrCreate(
                ['slug' => $categoryData['slug']],
                $categoryData
            );
        }

        // 创建子分类
        $techCategory = Category::where('slug', 'tech')->first();
        if ($techCategory) {
            $subCategories = [
                [
                    'name' => 'PHP开发',
                    'slug' => 'php',
                    'description' => 'PHP相关的开发技术和框架',
                    'parent_id' => $techCategory->id,
                ],
                [
                    'name' => 'JavaScript',
                    'slug' => 'javascript',
                    'description' => 'JavaScript前端开发技术',
                    'parent_id' => $techCategory->id,
                ],
                [
                    'name' => '数据库',
                    'slug' => 'database',
                    'description' => '数据库设计和优化相关',
                    'parent_id' => $techCategory->id,
                ],
            ];

            foreach ($subCategories as $subCategoryData) {
                Category::firstOrCreate(
                    ['slug' => $subCategoryData['slug']],
                    $subCategoryData
                );
            }
        }
    }
}